﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace ASPNetPatterns.Helpers
{
    public static class ExceptionsHelper
    {
        public static ASPNetPatterns.Models.IdentityAspException ReadIdentityResultErrorsAndReturnException(IEnumerable<string> errors) 
        {
            var count = errors.Count();
            string[] errorsArray = errors.ToArray();

            ASPNetPatterns.Models.IdentityAspException ex = new ASPNetPatterns.Models.IdentityAspException("No se ha registrar al usuario.");

            for (int i = 0; i < count; i++)
            {
                ex.Data.Add(String.Format("error-{0}", errorsArray[i]),errorsArray[i]);
            }

            return ex;
        }

        public static string GetExceptionDataAsString(this Exception exception) 
        {
            StringBuilder sb = new StringBuilder();
            foreach (string item in exception.Data.Values)
            {
                sb.Append(item);
            }

            return sb.ToString();
        }
    }
}